//
//  YFStarVideoController.m
//  SquashSpark
//
//  Created by Coollang on 2017/10/31.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFStarVideoController.h"
#import "YFStarVideoModel.h"
#import "YFStarCell.h"
#import "YFStarVideoSubController.h"
#import "JCVideoApi.h"
#import "WSProgressHUD.h"

NSString *const subVcSegueIndeifier = @"StarVideoSubSegue";

@interface YFStarVideoController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *dataSourceArr;
@property (nonatomic, strong) WSProgressHUD *hud;

@end

@implementation YFStarVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = kAutoHei(217);
    [self loadData];
}

- (void)loadData{
    __weak typeof(self)weakSelf = self;
    [JCVideoApi getVideoNodesComplete:^(NSError *error, NSArray <YFStarVideoModel *> *videoNodes) {
        if (error) {
            [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:2];
        }else{
            [weakSelf.hud dismiss];
            weakSelf.dataSourceArr = videoNodes;
            [weakSelf.tableView reloadData];
        }
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:subVcSegueIndeifier]) {
        YFStarVideoSubController *subVideoVc =   segue.destinationViewController;
        subVideoVc.videoModel = sender;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFStarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFStarCell" forIndexPath:indexPath];
    cell.starVideoModel = _dataSourceArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YFStarVideoModel *model = self.dataSourceArr[indexPath.row];
    if (model.videosCount > 0) {
        [self performSegueWithIdentifier:subVcSegueIndeifier sender:model];
    }
}

- (WSProgressHUD *)hud{
    if (_hud == nil) {
        _hud = [WSProgressHUD showOnView:self.view andString:@""];
        [_hud showWithMaskType:WSProgressHUDMaskTypeGradient];
        [_hud dismiss];
    }
    return _hud;
}


@end
